package com.gtm.controller;

import com.gtm.model.Pager;
import com.gtm.model.Topic;
import com.gtm.model.User;
import com.gtm.repository.TopicRepository;
import com.gtm.repository.UserRepository;
import com.gtm.service.TopicService;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javafx.scene.input.DataFormat;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import sun.dc.pr.PRError;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by 骆清波 on 2017/10/28.
 */
@RestController
@RequestMapping("/topic")
public class TopicController {
    @Autowired
    private TopicRepository tpr;

    @Autowired
    private TopicService tps;

    @Autowired
    private UserRepository userRepository;


    //老师保存上传课题
    @ApiOperation(value="save", notes="老师提交课题",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "topicName",value = "课题名称",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "topicSrc",value = "课题来源",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "topicType",value = "课题类型",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "requirement",value = "要求",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "introduction",value = "介绍",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "limitPerson",value = "限制人数",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "tid",value = "老师id",dataType = "String",paramType = "path")
    } )
    @RequestMapping("/save/{tid}")
    @RequiresRoles({"teacher"})
    Topic saveTopic(@RequestParam("topicName")String topicName,
                    @RequestParam("topicSrc")String topicSrc, @RequestParam("topicType")String topicType,
                    @RequestParam("requirement")String requirement, @RequestParam("introduction")String introduction,
                    @RequestParam("limitPerson")String limitPerson , @PathVariable String tid) throws ParseException {
        User teacher=userRepository.findOne(tid);
        String teacherName=teacher.getRealname();
        DBObject detail=teacher.getDetail();
        String tel=detail.get("phone").toString();
        return tps.saveTopic(tid,teacherName,tel,topicName,topicSrc,topicType,requirement,introduction,limitPerson);
    }

    //113 保存以及编辑课题
    @ApiOperation(value="saveAndUpdate", notes="编辑课题",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "tid",value = "老师id",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "topicName",value = "课题名称",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "topicSrc",value = "课题来源",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "topicType",value = "课题类型",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "requirement",value = "要求",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "introduction",value = "介绍",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "limitPerson",value = "限制人数",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "tpid",value = "TopicId",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "finalTime",value = "结束时间",dataType = "String",paramType = "query")
    } )
    @RequestMapping("/saveAndUpdate/{tid}")
    Topic saveAndUpdate(@RequestParam("topicName")String topicName,
                    @RequestParam("topicSrc")String topicSrc, @RequestParam("topicType")String topicType,
                    @RequestParam("requirement")String requirement, @RequestParam("introduction")String introduction,
                    @RequestParam("limitPerson")String limitPerson , @RequestParam("finalTime")Date finalTime,@RequestParam("tpid")String tpid, @PathVariable String tid) throws ParseException {
        User teacher=userRepository.findOne(tid);
        String teacherName=teacher.getRealname();
        DBObject detail=teacher.getDetail();
        String tel=detail.get("phone").toString();
        return tps.saveAndUpdate(tid,teacherName,tel,topicName,topicSrc,topicType,requirement,introduction,limitPerson,finalTime,tpid);
    }

    //113 根据课题id删除topic
    @ApiOperation(value="deleteByTpid", notes="id删除课题",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "tpid",value = "课题id",dataType = "String",paramType = "path"),
    } )
    @RequestMapping("/deleteByTpid/{tpid}")
    void deleteByTpid(@PathVariable String tpid){
        tpr.delete(tpid);
    }

    //113 根据老师id删除topic
    @ApiOperation(value="deleteByTid", notes="根据老师删除topic",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "tid",value = "老师id",dataType = "String",paramType = "path"),
    } )
    @RequestMapping("/deleteByTid/{tid}")
    void deleteByTid(@PathVariable String tid){
        tps.deleteByTid(tid);
    }



    //113 根据老师id查找topic 或者根据老师id以及一个searchkey查找
    @ApiOperation(value="listSelf", notes="根据老师以及条件查询课题",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "tid",value = "老师id",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "size",value = "每页条数",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "page",value = "第几页",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "searchKey",value = "查询条件",dataType = "String",paramType = "path")
    } )
    @RequestMapping(value = {"/listSelf/{tid}/{size}/{page}", "/listSelf/{tid}/{size}/{page}/{searchKey}"})
    Page<Topic> ListOwnTopic(@PathVariable int size,@PathVariable int page,@PathVariable(required = false) String searchKey,
                            @PathVariable String tid){
        return tps.listOwnTopic(new PageRequest(page,size, Sort.Direction.DESC,"topicType"),searchKey,tid);

    }


    //编辑topic
    @ApiOperation(value="updateTopic", notes="更新Topic",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "tpid",value = "课题id",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "topicName",value = "课题名称",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "topicSrc",value = "课题来源",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "topicType",value = "课题类型",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "requirement",value = "要求",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "introduction",value = "介绍",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "limitPerson",value = "限制人数",dataType = "String",paramType = "query"),
    } )
    @RequestMapping("/updateTopic/{tpid}")
    Topic updateTopic(@RequestParam("topicName")String topicName,
                      @RequestParam("topicSrc")String topicSrc,@RequestParam("topicType")String topicType,
                      @RequestParam("requirement")String requirement,@RequestParam("introduction")String introduction,
                      @RequestParam("limitPerson")String limitPerson ,@PathVariable String tpid){
        return tps.updateTopic(tpid,topicName,topicSrc,topicType,requirement,introduction,limitPerson);
    }

    //用于我的选题 各种查询条件都能满足
    @ApiOperation(value="listWithKeyMeta", notes="究极查询",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "size",value = "行数",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "page",value = "页数",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "searchKey",value = "条件",dataType = "String",paramType = "path")
    } )
    @RequiresRoles(value = {"teacher","student","director"},logical = Logical.OR)
    @RequestMapping(value = {"/listWithKeyMeta/{size}/{page}"})
    Pager<Topic> listWithKeyMeta(@PathVariable Integer size, @PathVariable Integer page, @RequestBody(required = false) String searchKey){
        System.out.println(searchKey);
        return tps.listWithKeyMeta(new PageRequest(page,size, Sort.Direction.DESC,"topicType"),searchKey);

    }

    //根据是否通过以及searchkey查询topic
    @ApiOperation(value="listWithKey", notes="查询课题",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "size",value = "行数",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "page",value = "页数",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "searchKey",value = "条件",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "isPassed",value = "是否通过",dataType = "String",paramType = "path")
    } )
    @RequestMapping(value = {"/listWithKey/{size}/{page}/{isPassed}/{searchKey}"})
    List<Topic> listWithKey(@PathVariable int size, @PathVariable int page, @PathVariable(required = false) String searchKey,@PathVariable(required = false)  String isPassed ){
        return tps.listWithKey(new PageRequest(page,size, Sort.Direction.DESC,"topicType"),searchKey,isPassed);
    }

    //通过或者驳回topic
    @ApiOperation(value="passTopic", notes="通过课题",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "tpid",value = "课题id",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "flag",value = "是否通过",dataType = "String",paramType = "path")
    } )
    @RequestMapping(value = {"/passTopic/{tpid}/{flag}"})
    Topic passTopic(@PathVariable String tpid,@PathVariable Boolean flag){
        Boolean flag1=Boolean.valueOf(flag);
        return tps.passTopic(tpid,flag1);
    }

    //根据是否通过和id查找一个topic
    @ApiOperation(value="selectTopic", notes="根据是否通过和id查找一个topic",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "tpid",value = "课题id",dataType = "String",paramType = "path")
    } )
    @RequestMapping(value = {"/selectTopic/{tpid}"})
    Topic selectTopic(@PathVariable String tpid,@RequestParam(required = false,value = "flag") Boolean flag){
        if (flag==null)
            return tpr.findOne(tpid);
        Boolean flag1=Boolean.valueOf(flag);
        return tps.selectTopic(tpid,flag1);
    }

    //学生申请topic
    @ApiOperation(value="applyTopic", notes="学生申请topic",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "tpid",value = "课题id",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "stuId",value = "学生id",dataType = "String",paramType = "path")
    } )
    @RequestMapping(value = "/applyTopic/{topicId}/{stuId}")
    Topic applyTopic(@PathVariable String topicId,@PathVariable String stuId){
        return tps.applyTopic(topicId,stuId);
    }


    //113
    @ApiOperation(value="finishTopic", notes="老师提交课题 结束流程",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "topicId",value = "课题id",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "stuList",value = "学生列表 ",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "delList",value = "置空学生列表 ",dataType = "String",paramType = "query")
    } )
    @RequestMapping(value = "/finishTopic/{topicId}")
    Topic finishTopic(@PathVariable String topicId,@RequestParam(value = "stuList",required = false)String[] stuList,@RequestParam(value = "delList",required = false)String[] delList){
        List<String> stList= Arrays.asList(stuList);
        List<String> deList= Arrays.asList(delList);
        return tps.finishTopic(topicId,stList,deList);
    }

    //113
    @ApiOperation(value="cancelChooseTopic", notes="学生取消选题",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "topicId",value = "课题id",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "username",value = "学生学号 ",dataType = "String",paramType = "path")
    } )
    @RequestMapping(value = "/cancelChooseTopic/{topicId}/{username}")
    Topic cancelChooseTopic(@PathVariable String topicId,@PathVariable String username){
        return tps.cancelChooseTopic(topicId,username);    }

    //113
    @ApiOperation(value="chooseNewTopic", notes="选择新课题",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "topicId",value = "课题id",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "username",value = "学生学号 ",dataType = "String",paramType = "path")
    } )
    @RequestMapping(value = "/chooseNewTopic/{topicId}/{username}")
    Topic chooseNewTopic(@PathVariable String topicId,@PathVariable String username){
        return tps.chooseNewTopic(topicId,username);
    }

    @ApiOperation(value="listSelfIsPass", notes="列出老师自己的是否通过的课题",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "tid",value = "老师id",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "flag",value = "是否通过 ",dataType = "String",paramType = "query")
    } )
    @RequestMapping(value = {"/listSelfIsPass/{tid}"})
    List<Topic> listSelfIsPass(@PathVariable String tid,@RequestParam(value = "flag",required = false)String flag){
        if(null==flag)
            return tpr.findByTeacherId(tid);
        return tpr.findByTeacherIdAndMarkedDel(tid,Boolean.valueOf(flag));
    }

    @ApiOperation(value="findById", notes="通过课题id查找",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "topicId",value = "课题id",dataType = "String",paramType = "path")
    } )
    @RequestMapping(value = "/findById/{topicId}")
    Topic chooseNewTopic(@PathVariable String topicId){
        return tpr.findOne(topicId);
    }

    @ApiOperation(value="findNameById", notes="通过课题id查找课题名",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "topicId",value = "课题id",dataType = "String",paramType = "path")
    } )
    @RequestMapping(value = "/findNameById/{topicId}")
    String findNameById(@PathVariable String topicId){
        return tpr.findOne(topicId).getTopicName();
    }

}
